import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import {article,comment}  from "@/services/index"
export interface IndexModelState {
  name: string;
}

export interface IndexModelType {
  namespace: 'home';
  state: IndexModelState;
  effects: {
    query: Effect;
    comment:Effect;
  };
  reducers: {
    save: Reducer<IndexModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions: { setup: Subscription };
}

const IndexModel: IndexModelType = {
  namespace: 'home',

  state: {
    name: '',
    
  },
  effects: {
    //   
    *query({ payload }, { call, put }) {
        let res=yield call (article);
              yield put({
                  type:"save",
                  payload:{
                      artiList:res.data[0]
                  }
              })
         
    },
    *comment({ payload }, { call, put }) {
        let res=yield call (comment);
              console.log(res.data[0]);
              yield put({
                  type:"save",
                  payload:{
                      commentLisr:res.data[0]
                  }
              })
             
    },

    
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/') {
          dispatch({
            type: 'query',
          });
        }
      });
    },
  },
};

export default IndexModel;
function commentLisr(commentLisr: any) {
  throw new Error('Function not implemented.');
}

